package com.bstek.bdf2.core.orm.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @Title: SessionFactoryConfig
 * @Package: com.bstek.bdf2.core.orm.config
 * @Description:
 * @Author: zxq
 * @Date: 2024/1/2 - 21:17
 */
//@Configuration
public class SessionFactoryConfig {

//    @Autowired
    private DataSource dataSourceOne;

//    @Bean
    public SessionFactory sessionFactory() throws Exception {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSourceOne);
        sessionFactoryBean.setPackagesToScan("com.bstek.*");
        sessionFactoryBean.setHibernateProperties(hibernateProperties());
        sessionFactoryBean.afterPropertiesSet();
        return sessionFactoryBean.getObject();
    }

    public Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.setProperty("hibernate.show_sql", "false");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.allow_update_outside_transaction", "true");
        return properties;
    }
}
